package org.electrical.controller.functionality;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.electrical.dao.EleCustomerDao;
import org.electrical.dao.EleLossDao;
import org.electrical.dao.EleOrderDao;
import org.electrical.pojo.EleLoss;
import org.electrical.pojo.EleOrder;
import org.electrical.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
@CrossOrigin(value = "*")
public class EleOrderController {

    @Autowired
    private EleOrderDao eleOrderDao;
    @Autowired
    private EleLossDao eleLossDao;
    @Autowired
    private EleCustomerDao eleCustomerDao;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult<List<EleOrder>> list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, String cusId) throws Exception {

        Page<EleOrder> pager = PageHelper.startPage(page, limit);
        List<EleOrder> list = eleOrderDao.getByCusId(cusId);
        return new ResponseResult(pager.getTotal(), list);

    }

    @RequestMapping("/lossCus")
    @ResponseBody
    public ResponseResult<List<EleOrder>> getLossCus() throws Exception {

        List<EleOrder> list = eleOrderDao.getLossCus();

        EleLoss eleLoss = new EleLoss();
        if (list != null) {
            for (EleOrder loss : list) {
                eleLoss.setCustomerId(loss.getCustomerId());
                eleLoss.setLastTime(loss.getOrderDate());
            }
            eleCustomerDao.early(eleLoss.getCustomerId());
            eleLossDao.add(eleLoss);
        }

        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/offer")
    @ResponseBody
    public ResponseResult offer(String name) throws Exception {

        List<EleOrder> map = eleOrderDao.getOffer(name);
        return new ResponseResult(map);

    }

}
